﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Enterprises.Wpf.Sample.Data;
using Enterprises.Wpf.Sample.Messages;
using Enterprises.Wpf.Sample.Tools.Helper;

namespace Enterprises.Wpf.Sample.ViewModel.Main
{
    /// <summary>
    /// 标题栏菜单
    /// </summary>
    public class NonClientAreaViewModel : ObservableObject
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public NonClientAreaViewModel()
        {
            VersionInfo = "企业级WPF应用实列 " + VersionHelper.GetVersion();
        }

        /// <summary>
        /// 菜单点击事件
        /// </summary>
        public RelayCommand<string> OpenViewCmd => new(OpenView);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cmdParam">菜单参数，结构：菜单组-菜单视图名称</param>
        private void OpenView(string cmdParam)
        {
            var viewData = cmdParam.Split('-');
            var viewName = cmdParam;
            var viewGroupName = string.Empty;
            if (viewData.Length == 2)
            {
                viewName = viewData[1];
                viewGroupName = viewData[0];
            }

            WeakReferenceMessenger.Default.Send(new LeftMenuChangedMessage(true, viewGroupName), MessageToken.ClearLeftSelected);
            WeakReferenceMessenger.Default.Send(new FullSwitchMessage(true), MessageToken.FullSwitch);
            WeakReferenceMessenger.Default.Send(AssemblyHelper.CreateInternalInstance($"UserControls.Controls.{viewName}"), MessageToken.LoadShowContent);
        }

        private string _versionInfo;

        public string VersionInfo
        {
            get => _versionInfo;
            set => SetProperty(ref _versionInfo, value);
        }
    }
}
